﻿// owner 蒋家伟

#include "SocketHardwareCompoent.h"

// qt
#include <QThread>

// jsdk
#include "TaskId.h"
#include "Dbg_Warn_Define.h"

using namespace jsdk;

OwnerPtr<jsdk::SocketHardwareCompoent> jsdk::SocketHardwareCompoent::create(IHardwareEventHandler* pHardwareEventHandler, ISocketEventHandler* pSocketHandler)
{
    OwnerPtr<jsdk::SocketHardwareCompoent> opSocketHardwareCompoent = NEW_AS_OWNERPTR(SocketHardwareCompoent);
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(opSocketHardwareCompoent, L"套接字硬件组件创建失败", L"蒋家伟", L"2023-08-03");
    opSocketHardwareCompoent->m_opHardwareWorker.swap(SocketHardwareWorker::create(pHardwareEventHandler));
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(opSocketHardwareCompoent->m_opHardwareWorker, L"套接字硬件工作对象创建失败", L"蒋家伟", L"2023-08-03");
    opSocketHardwareCompoent->m_opSocket.swap(ISocket::create(pSocketHandler));
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(opSocketHardwareCompoent->m_opSocket, L"套接字套接字对象创建失败", L"蒋家伟", L"2023-08-03");
    opSocketHardwareCompoent->m_opThread.swap(NEW_AS_OWNERPTR_WITH_DELETER(QThread, ThreadDeleter));
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(opSocketHardwareCompoent->m_opThread, L"硬件对象工作线程创建失败", L"蒋家伟", L"2023-08-03");
    return std::move(opSocketHardwareCompoent);
}

bool jsdk::SocketHardwareCompoent::start(const std::string& ip, int64_t port)
{
    DBG_WARN_AND_RETURN_FALSE_UNLESS(m_opSocket && !m_opSocket->isOnLine(), L"硬件套接字正在工作中请勿重复开始", L"蒋家伟", L"2023-07-28");
    DBG_WARN_AND_RETURN_FALSE_UNLESS(m_opThread, L"工作线程未初始化", L"蒋家伟", L"2023-07-28");
    DBG_WARN_AND_RETURN_FALSE_UNLESS(m_opHardwareWorker, L"工作对象未初始化", L"蒋家伟", L"2023-07-28");
    // 硬件Work工作开始
    bool success = SocketHardwareCompoent::init();

    DBG_WARN_AND_RETURN_FALSE_UNLESS(success, L"硬件对象初始化失败 ———— [信号与槽链接]", L"蒋家伟", L"2023-07-28");
    m_opHardwareWorker->moveToThread(m_opThread.get());
    m_opThread->start();

    // 使用的套接字开始工作
    m_opSocket->connected(ip, port);
    return true;
}

bool jsdk::SocketHardwareCompoent::reConnect(const std::string& ip, int64_t port) const
{
    DBG_WARN_AND_RETURN_FALSE_UNLESS(m_opSocket && !m_opSocket->isOnLine(), L"硬件套接字正在工作中请勿重复连接", L"蒋家伟", L"2023-07-28");
    DBG_WARN_AND_RETURN_FALSE_UNLESS(m_opThread, L"工作线程未初始化", L"蒋家伟", L"2023-07-28");
    DBG_WARN_AND_RETURN_FALSE_UNLESS(m_opHardwareWorker, L"工作对象未初始化", L"蒋家伟", L"2023-07-28");
    m_opSocket->reconnect(ip, port);
    return true;
}

void jsdk::SocketHardwareCompoent::stop()
{
    // 可以暂停线程或者停止所有任务，或者关闭从来
}

void jsdk::SocketHardwareCompoent::addTask(const TaskId& taskId) const
{
    DBG_WARN_AND_RETURN_VOID_UNLESS(m_opSocket, L"套接字未工作", L"蒋家伟", L"2023-07-28");
    DBG_WARN_AND_RETURN_VOID_UNLESS(m_opThread, L"工作线程未初始化", L"蒋家伟", L"2023-07-28");

    QVariant data;
    data.setValue(taskId);
    emit addTasks(data);
}

void jsdk::SocketHardwareCompoent::sendMessage(std::string && msg, int64_t length) const
{
    DBG_WARN_UNLESS(m_opSocket && m_opSocket->isOnLine(), L"套接字对象为工作", L"蒋家伟", L"2023-08-03");
    return m_opSocket->send(std::move(msg), length);
}

bool jsdk::SocketHardwareCompoent::isOnline() const
{
    if (m_opSocket)
    {
        return m_opSocket->isOnLine();
    }
    return false;
}

bool jsdk::SocketHardwareCompoent::init() const
{
    // 连接信号与槽
    DBG_WARN_AND_RETURN_FALSE_UNLESS(m_opHardwareWorker, L"硬件工作对象创建错误", L"蒋家伟", L"2023-07-28");
    bool success = connect(this, SIGNAL(addTasks(QVariant)), m_opHardwareWorker.get(), SLOT(onAddTask(QVariant)));
    DBG_WARN_AND_RETURN_FALSE_UNLESS(success, L"硬件开始工作的信号与槽连接失败", L"蒋家伟", L"2023-07-26");
    return true;
}
